package app.kernel.psgManager;

import java.util.Hashtable;
import java.util.Arrays;

public class PSG_Registry {

	private static Hashtable PSGTable;
	
	public static Heap PSG_References;
	public static int Size;
	
	public PSG_Registry(){
		
		PSGTable = new Hashtable();
		PSG_References = new Heap(10000);
		Size=0;
		
	}
	
	public boolean addPSG(PhysicalSpace PS){
		
		   PSGTable.put(PS.getName(), PS);		
		   PSG_References.enqueue(PS);
		   Size++;
		   return true;
		
	}
	
	public PhysicalSpace getPSG(String PSName){
		
		return (PhysicalSpace)PSGTable.get(PSName);
		
	}
	
	public boolean hasPSG(String PSName){
		
		if (PSGTable.containsKey(PSName))
			
		    return true;
		
		else
			
			return false;
		
	}
	
	public String getRandomPeerReference(){
		
		return PSG_References.getRandomPeerReference();
		
	}
	
	// For registering PSG to get a list of peers references
	public String[] getListOfPSG(){
		
		String[] result = new String[10];
		PhysicalSpace[] PS = new PhysicalSpace[10];
		int numPeers=10;
		
		if (Size<10) {
			
			numPeers = Size;
			
		}
		
		for (int i=0; i<numPeers; i++){
			
			PS[i] = ((PhysicalSpace)PSG_References.dequeue());
			result[i] = PS[i].getAddress(); 
		}
		
		for (int i=0; i<numPeers; i++){
			
			PSG_References.enqueue(PS[i]);
			
		}
		
		
		return result;		
		
	}
	
}